<?php
/**
 * 获取代理参数(代理地址和端口)
 * 返回是否启用代理
 */
function get_proxy_param(&$proxyHost, &$proxyPort,&$username,&$password)
{
    $cfg = config("proxy.");
    if ($cfg['enable']) {
        $proxyHost = $cfg['proxy_host'];
        $proxyPort = $cfg['proxy_port'];
        $username=$cfg['username'];
        $password=$cfg['password'];
        return true;
    } else {
        return false;
    }
}

/**
 * 使用服务器直接发起一个Http GET请求
 *
 * @param [type] $url
 * @return void
 */
function curl_get($url)
{
    $headers = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36');
    $oCurl = curl_init();
    if (get_proxy_param($proxy,$proxyport,$username,$password)) {
        curl_setopt($oCurl, CURLOPT_PROXY, $proxy);
        curl_setopt($oCurl, CURLOPT_PROXYPORT, $proxyport);
        if ($username){
            curl_setopt($oCurl,CURLOPT_PROXYUSERPWD,$username.':'.$password);
        }
    }
    if (stripos($url, "https://") !== false) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    
    curl_setopt($oCurl, CURLOPT_TIMEOUT, 20);
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

// /**
//  * 生成一个16字符长度的随机字符串
//  */
// function getRandomStr()
// {
//     $str = "";
//     $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
//     $max = strlen($str_pol) - 1;
//     for ($i = 0; $i < 16; $i++) {
//         $str .= $str_pol[mt_rand(0, $max)];
//     }
//     return $str;
// }
